//
//  UIViewController+DropAnimation.m
//  WeShare2.0
//
//  Created by KevinCao on 15/11/9.
//  Copyright © 2015年 WeShare. All rights reserved.
//

#import "UIViewController+DropAnimation.h"
#import <objc/runtime.h>

@implementation UIViewController (DropAnimation)

- (void)dropImage:(UIImageView *)imageView fromPostion:(CGPoint)startPositon toPosition:(CGPoint)endPosition
{
    UIWindow *window = [UIApplication sharedApplication].windows[0];
    UIImageView *screenShot = [[UIImageView alloc] initWithImage:[HDUtil screenShot:self.view]];
    [window addSubview:screenShot];
    window.userInteractionEnabled = NO;
    
    CALayer *layer = [CALayer layer];
    layer.contents = (__bridge id)imageView.image.CGImage;
    layer.contentsGravity = kCAGravityResizeAspectFill;
    layer.bounds = CGRectMake(0, 0, imageView.width, imageView.height);
    if (!imageView.layer.cornerRadius) {
        [layer setCornerRadius:CGRectGetHeight([layer bounds])/2];
    } else {
        [layer setCornerRadius:imageView.layer.cornerRadius];
    }
    layer.masksToBounds = YES;
    layer.position =CGPointMake(startPositon.x, startPositon.y);
    [screenShot.layer addSublayer:layer];
    
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, imageView.width+1, imageView.height+1)];
    view.center = startPositon;
    view.backgroundColor = [UIColor whiteColor];
    view.layer.cornerRadius = layer.cornerRadius;
    view.clipsToBounds = YES;
    [screenShot insertSubview:view atIndex:0];
    
    UIBezierPath *path = [UIBezierPath bezierPath];
    [path moveToPoint:CGPointMake(startPositon.x, startPositon.y)];
    [path addQuadCurveToPoint:CGPointMake(endPosition.x, endPosition.y) controlPoint:CGPointMake(endPosition.x-(endPosition.x-startPositon.x)/2.0, startPositon.y+10)];
    
    CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
    animation.path = path.CGPath;
//    animation.rotationMode = kCAAnimationRotateAuto;
    CABasicAnimation *expandAnimation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
    expandAnimation.duration = 0.5f;
    expandAnimation.fromValue = [NSNumber numberWithFloat:1];
    expandAnimation.toValue = [NSNumber numberWithFloat:0.6f];
    expandAnimation.timingFunction=[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn];

    CABasicAnimation *opacityAnim = [CABasicAnimation animationWithKeyPath:@"opacity"];
    opacityAnim.duration = 0.5;
    opacityAnim.fromValue = [NSNumber numberWithFloat:1.0];
    opacityAnim.toValue = [NSNumber numberWithFloat:0.1];
    opacityAnim.removedOnCompletion = YES;
    
    CAAnimationGroup *groups = [CAAnimationGroup animation];
    groups.animations = @[animation,expandAnimation,opacityAnim];
    groups.duration = 0.5;
    groups.removedOnCompletion=NO;
    groups.fillMode=kCAFillModeForwards;
    groups.delegate = self;
    [layer addAnimation:groups forKey:@"group"];
    
    [self performBlock:^{
        [UIView animateWithDuration:0.3 animations:^{
            screenShot.alpha = 0;
        } completion:^(BOOL finished) {
            window.userInteractionEnabled = YES;
            [screenShot removeFromSuperview];
        }];
    } afterDelay:0.5];
}

@end
